package com.example.homeworkdev.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.homeworkdev.R;
import com.example.homeworkdev.adapter.WaterfallAdapter;
import com.example.homeworkdev.entity.Item;
import com.example.homeworkdev.util.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class FindFragment extends Fragment {
    private RecyclerView recyclerView;
    private WaterfallAdapter adapter;
    private List<Item> itemList = new ArrayList<>();



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_find, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化 RecyclerView
        recyclerView = view.findViewById(R.id.recyclerView);

        // 设置瀑布流布局管理器（2列，垂直方向）
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        // 添加间隔
        recyclerView.addItemDecoration(new SpacesItemDecoration(8));

        // 初始化数据
        if (savedInstanceState == null) {
            //自定义生成图片数量
            initData(13);
            // 设置适配器
            adapter = new WaterfallAdapter(itemList);
        }

        recyclerView.setAdapter(adapter);
    }

    private void initData(int count) {
        // 模拟数据
        int[] imageResIds = {R.drawable.img1, R.drawable.img2, R.drawable.img3,
                R.drawable.img4, R.drawable.img5, R.drawable.img6,R.drawable.img7,
                R.drawable.img8,R.drawable.img9,R.drawable.img10,R.drawable.img11
                ,R.drawable.img12,R.drawable.img13};
        String[] titles = {"萌宠", "壁纸", "可爱", "汪星人", "柴犬", "捣蛋","柴犬","柴犬","喵星人","喵星人"
                ,"屯屯鼠","兔兔","喵星人"};
        Random random = new Random();

        for (int i = 0; i < count; i++) {
            int position = i % imageResIds.length;
            int height = 300 + random.nextInt(300);
            itemList.add(new Item(imageResIds[position], titles[position], height));
        }
    }
}